<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ModuleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request)
    {
        $data = DB::table('sa_model as mo')
            -> LeftJoin('sa_type as t','mo.t_type','=','t.id')
            -> LeftJoin('sa_supplier as sp','mo.t_su_id','=','sp.s_id')
            -> select('t.*','mo.*','sp.s_name')
            -> paginate(10);
        //dd($data);
        return view('admin.module.index',['data' => $data,'request'=>$request->all()]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getCreate()
    {
        //查询设备类型表
        $type = DB::table('sa_type') -> get();
        $data = DB::table('sa_supplier')
            -> select('s_id','s_name')
            -> get();
        return view('admin.module.create',['type'=>$type,'data'=>$data]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function postStore(Request $request)
    {
        $data = array();
        $input = $request->except('_token');
        $data['t_su_id'] = $input['su_id'];
        $data['t_type'] = $input['type']; 
        $data['t_model'] = $input['model'];
        $data['t_software'] = $input['software'];
        $data['t_hardware'] = $input['hardware'];
        @$insert = DB::table('sa_model') ->insert($data);
        if($insert){
            return redirect('admin/module/index')->with('success','添加成功');
        }else{
            return back() ->withInput() -> with('error','添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show()
    {
        // 
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getEdit(Request $request)
    {
        $id = $request -> get('id');
        $data = DB::table('sa_supplier')
            -> select('s_id','s_name')
            -> get();
        //查询设备类型表
        $type = DB::table('sa_type') -> get();
        $info = DB::table('sa_model as mo')
            -> LeftJoin('sa_supplier as sp','mo.t_su_id','=','sp.s_id')
            -> select('mo.*','sp.s_name')
            -> where('mo.tp_id','=',$id)
            -> first();
            // dd($info);
        return view('admin.module.edit',['data' => $data,'type'=>$type,'info'=>$info]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postUpdate(Request $request)
    {
        $data = array();
        $id = $request -> only('id');
        $input = $request -> except('_token','id');
        $data['t_su_id'] = $input['su_id'];
        $data['t_type'] = $input['type'];
        $data['t_model'] = $input['model'];
        $data['t_software'] = $input['software'];
        $data['t_hardware'] = $input['hardware'];
        $update = DB::table('sa_model') -> where('tp_id','=',$id) -> update($data);
        if($update){
             return redirect('admin/module/index')->with('success','修改成功');
        }else{
            return back() ->withInput() -> with('error','修改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
